

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:untitled/pages/Getx_project/CountController.dart';
class CountMainPage extends StatefulWidget {
  const CountMainPage({Key? key}) : super(key: key);

  @override
  State<CountMainPage> createState() => _CountMainPageState();
}
// GetX 状态管理基本使用
class _CountMainPageState extends State<CountMainPage> {
  int _count = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("计数器"),
      ),
      backgroundColor: Colors.white,
      body: Container(
        width: double.infinity,
        height:double.infinity,
        child:Column(
          children: [
             buildColumn(),
             buildColumn2(),
             buildColumn3(),
          ],
        )
      ),
    );
    
  }
  Column buildColumn(){
    return Column(
          children:[
            SizedBox(height: 30,),
            Text("当前显示计数$_count"),
            SizedBox(height: 10,),
            ElevatedButton(
              onPressed: (){
                setState(() {
                  _count++;
                });
              },
              child: Text("添加")
              )

          ]
        );
  }
  buildColumn2(){
    return Column(
      mainAxisSize: MainAxisSize.min,
          children:[
             SizedBox(height: 30,),
            // 第二步 使用GetBuilder 获取数据
            GetBuilder<CountController>(
              init: CountController(),
              builder: (CountController controller){
               return Text("当前显示计数${controller.count}");
            }),
            
            SizedBox(height: 10,),
            ElevatedButton(
              onPressed: (){
                // 第三步 使用find 调用方法通信
                Get.find<CountController>().add();
              },
              child: Text("添加")
              )

          ]
        );

  }

  buildColumn3(){
    return Column(
      mainAxisSize: MainAxisSize.min,
          children:[
             SizedBox(height: 30,),
            // 第二步 使用GetBuilder 获取数据
            GetBuilder<CountController>(
              init: CountController(),
              id: "count2",
              builder: (CountController controller){
               return Text("当前显示计数${controller.count2}");
            }),
            
            SizedBox(height: 10,),
            ElevatedButton(
              onPressed: (){
                // 第三步 使用find 调用方法通信
                Get.find<CountController>().addCount2();
              },
              child: Text("添加")
              )

          ]
        );

  }
}